<?php

class CustomerController extends AController
{
    public $_model;

    public function beforeAction($action)
    {
        return parent::beforeAction($action);
    }

    public function actionView()
    {
        $this->render('view', array(
            'model' => $this->loadModel(),
        ));
    }

    public function loadModel($id)
    {
        if ($this->_model === null) {
            $this->_model = Users::model()->findbyPk($id);
            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->_model;
    }

    public function actionCreate()
    {
        $model = new Users;

        if (isset($_POST['Users'])) {
        	$_POST['Users']['repeat_password'] = $_POST['Users']['password'];
            $model->attributes = $_POST['Users'];
            if($model->save()) {
                $this->redirect(array('/admin/customer/admin'));
            } 
        }
        
        $criteria = new CDbCriteria();
        $criteria->order = 'name asc';
        $countryList = Country::model()->findAll($criteria);
        
        $this->render('create', array(
            'model' => $model,
        	'countryList' => $countryList,
        ));
    }

    public function actionUpdate($id)
    {
        $customer = $this->loadModel($id);

        if (isset($_POST['Customer'])) {
        	if($_POST['Customer']['password'] == '') {
        		unset($_POST['Customer']['password']);
        	} else {
        		$model->oldPassword = $model->password;
        	}
            $customer->attributes = $_POST['Customer'];
            $customer->country_id = $_POST['Customer']['country_id'];
            $customer->updated_time = date('Y-m-d H:i:s');

            try {
            	$result = $customer->update($customer);
            } catch (Exception $e) {
            	echo $e->getMessage();exit;
            }
            
            $this->redirect(array('/admin/customer/admin'));
        }
	
        $criteria = new CDbCriteria();
        $criteria->order = 'name asc';
        $countryList = Country::model()->findAll($criteria);
    
        $this->render('update', array(
            'customer' => $customer,
        	'countryList' => $countryList,
        ));
    }

    public function actionDelete($id)
    {
        try {
            $this->loadModel($id)->delete();
        } catch (CDbException $e) {
            Yii::app()->user->setFlash('error', "Khách hàng này đã có trong danh sách order, không thể xóa!");
        }
        $this->redirect(array('/admin/customer/admin'));
    }

    public function actionAdmin()
    {
    /*
        if (Yii::app()->session['customer_keyword']) {
            unset(Yii::app()->session['customer_keyword']);
        }
        $sort = 'customer_id desc';

        $criteria = new CDbCriteria();
        if (!empty($_POST['customer_keyword'])) {
            $keyword = $_POST['customer_keyword'];
        }
        $criteria->order = $sort;

        $item_count = Customer::model()->count($criteria);

        $pages = new CPagination($item_count);
        $pages->setPageSize(Yii::app()->params['listPerPage']);
        $pages->applyLimit($criteria); // the trick is here!

        $this->render('admin', array(
            'model' => Customer::model()->findAll($criteria), // must be the same as $item_count
            'item_count' => $item_count,
            'page_size' => Yii::app()->params['listPerPage'],
            'items_count' => $item_count,
            'pages' => $pages,
            'sort' => $sort
        ));
    */
        $sort = 'created_time desc';
        $criteria = new CDbCriteria();
        $criteria->order = $sort;
        $criteria->condition = 'role is null';

        $item_count = Users::model()->count($criteria);

        $pages = new CPagination($item_count);
        //$pages->setPageSize(Yii::app()->params['listPerPage']);
        $pages->applyLimit($criteria);  // the trick is here!
        $this->render('admin',array(
            'model'=> Users::model()->findAll($criteria), // must be the same as $item_count
            'item_count'=>$item_count,
            'page_size'=>Yii::app()->params['listPerPage'],
            'items_count'=>$item_count,
            'pages'=>$pages,
            'sort'=>$sort
        ));
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'customer-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
